package com.backend.demo.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.backend.demo.baseDAO.BaseDao;
import com.backend.demo.entities.Permission;
import com.backend.demo.util.DateTimeUtil;
import com.backend.demo.util.StringUtils;

/**
 * 权限DAO
 * @author Administrator
 *
 */
@SuppressWarnings("unchecked")
@Repository
public class PermissionDao extends BaseDao<Permission, Long> {
	
	/**
	 * 获得所有的权限信息
	 * @return
	 */
	public List<Permission> getPermissions(){
		return getListByHQL("from Permission");
	}
	
	/**
	 * 综合查询权限信息
	 * @param permissionName
	 * @param createtime
	 * @return
	 */
	public List<Permission> getPermissionByCondition(String permissionName,Date createtime){
		StringBuffer stringBuffer = new StringBuffer("from Permission where 1=1");
		if(StringUtils.isNotEmpty(permissionName)){
			stringBuffer.append("and permissionName like '%" + permissionName + "%'");
		}
		if(createtime != null){
			String format = DateTimeUtil.format("yyyy-MM-dd HH:mm:ss", createtime);
			stringBuffer.append("and createtime >='"+format+"'");
		}
		return getListByHQL(stringBuffer.toString());
	}
	
	/**
	 * 添加权限
	 * @param permission
	 */
	public int addPermission(Permission permission){
		permission.setCreatetime(new Date());
		permission.setUpdatetime(new Date());
		return save(permission);
	}
	
	/**
	 * 修改权限
	 * @param permissionID
	 * @param permissionName
	 * @return
	 */
	public int updatePermission(Long permissionID, String permissionName){
		String hql = "update Permission set permissionName = ?,updatetime = ? where permissionID = ?";
		return updateOrDeleteByHql(hql,permissionName, new Date(),permissionID);
	}
	
	/**
	 * 删除一个
	 * @param ids
	 */
	public int delete(Long id){
		String hql = "delete from Permission where permissionID = ?";
		return updateOrDeleteByHql(hql,id);
	}
	
	public void deleteBatch(Long[] ids){
		deleteBatchById(ids);
	}
	
	/**
	 * 根据权限ID查询权限信息
	 * @param permissionID
	 * @return
	 */
	public Permission getPermissionById(long permissionID){
		String hql = "from Permission where permissionID = ?";
		return getOneByHQL(hql, permissionID);
	}
}
